package com.gdpu.project.action.achievementManager;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.gdpu.page.vo.PageView;
import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.service.ChengGuoService;
import com.gdpu.project.service.TeacherService;
import com.gdpu.project.util.DateUtil;
import com.gdpu.project.vo.CgPatent;
import com.gdpu.project.vo.Teacher;
import com.opensymphony.xwork2.ActionSupport;

public class PatentAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpSession session;
	private int id;
	private String patentId;
	private String applyDate;
	private String teacherId;
	private String fuZeRen;
	private String patentTitle;
	private String obtainCertificateTime;
	private String patentType;
	private float keYanFen;
	private float reword;
	private String shouYiHao;
	private String xueKe;
	private String schoolSign;
	private String publishRange;
	private String comment;
	private TeacherService teacherService;
	private ChengGuoService chengguoService;
	private PageView<CgPatent> pageView;
	private int currentpage=1;

	public PageView<CgPatent> getPageView() {
		return pageView;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	public void setChengguoService(ChengGuoService chengguoService) {
		this.chengguoService = chengguoService;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getPatentId() {
		return patentId;
	}

	public void setPatentId(String patentId) {
		this.patentId = patentId;
	}

	public String getApplyDate() {
		return applyDate;
	}

	public void setApplyDate(String applyDate) {
		this.applyDate = applyDate;
	}

	public String getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}

	public String getPatentTitle() {
		return patentTitle;
	}

	public void setPatentTitle(String patentTitle) {
		this.patentTitle = patentTitle;
	}

	public String getShouYiHao() {
		return shouYiHao;
	}

	public void setShouYiHao(String shouYiHao) {
		this.shouYiHao = shouYiHao;
	}

	public String getPatentType() {
		return patentType;
	}

	public void setPatentType(String patentType) {
		this.patentType = patentType;
	}

	public String getXueKe() {
		return xueKe;
	}

	public void setXueKe(String xueKe) {
		this.xueKe = xueKe;
	}

	public String getSchoolSign() {
		return schoolSign;
	}

	public void setSchoolSign(String schoolSign) {
		this.schoolSign = schoolSign;
	}

	public String getPublishRange() {
		return publishRange;
	}

	public void setPublishRange(String publishRange) {
		this.publishRange = publishRange;
	}

	public float getKeYanFen() {
		return keYanFen;
	}

	public void setKeYanFen(float keYanFen) {
		this.keYanFen = keYanFen;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getFuZeRen() {
		return fuZeRen;
	}

	public String getObtainCertificateTime() {
		return obtainCertificateTime;
	}

	public float getReword() {
		return reword;
	}

	public void setFuZeRen(String fuZeRen) {
		this.fuZeRen = fuZeRen;
	}

	public void setObtainCertificateTime(String obtainCertificateTime) {
		this.obtainCertificateTime = obtainCertificateTime;
	}

	public void setReword(float reword) {
		this.reword = reword;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	// 增加专利
	public String addPatent() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		CgPatent cgPatent = new CgPatent();
		cgPatent.setFuZeRen(fuZeRen);
		cgPatent.setObtainCertificateTime(DateUtil.stringToDate(obtainCertificateTime));
		cgPatent.setReword(reword);
		cgPatent.setApplyDate(DateUtil.stringToDate(getApplyDate()));
		cgPatent.setComment(getComment());
		cgPatent.setKeYanFen(getKeYanFen());
		cgPatent.setPatentId(getPatentId());
		cgPatent.setPatentTitle(getPatentTitle());
		cgPatent.setPatentType(getPatentType());
		cgPatent.setPublishRange(getPublishRange());
		cgPatent.setSchoolSign(getSchoolSign());
		cgPatent.setShouYiHao(getShouYiHao());
		cgPatent.setXueKe(getXueKe());
		//set对应的教师编号
		List<Teacher> teacherList=teacherService.findByName(fuZeRen);
		if(teacherList.size()>0){
			cgPatent.setTeacherId(teacherList.get(0).getTeacherId());
		}
		chengguoService.addCgPatent(cgPatent);//findAllWritingsDepRole
		List<CgPatent> cgPatentlist = findAllPatentDepRole();
		
		request.setAttribute("cgPatentlist", cgPatentlist);
		return SUCCESS;
	}


	
	// 根据id查找专利
	public String findPatentById() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		CgPatent cgPatent = chengguoService.findCgPatent(getId());
		request.setAttribute("cgPatent", cgPatent);
		return SUCCESS;
	}

	// 根据id删除专利
	public String delPatentById() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		
		chengguoService.deleteCgPatentById(getId());
		pageView=new PageView<CgPatent>(10,currentpage);
		//角色判断，管理员返回所有，教职工返回相应的			
		List<CgPatent> cgPatentlist = findAllPatentDepRole();
		request.setAttribute("cgPatentlist", cgPatentlist);
		return SUCCESS;
	}

	// 查找所有专利
	public String findAllPatent() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		List<CgPatent> cgPatentlist = findAllPatentDepRole();
		
		request.setAttribute("cgPatentlist", cgPatentlist);
		return SUCCESS;
	}

	// 更新专利
	public String updatePatent() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		CgPatent cgPatent = chengguoService.findCgPatent(getId());
		if (cgPatent != null) {
			cgPatent.setObtainCertificateTime(DateUtil.stringToDate(obtainCertificateTime));
			cgPatent.setFuZeRen(fuZeRen);
			cgPatent.setReword(reword);
			cgPatent.setComment(comment);
			cgPatent.setKeYanFen(keYanFen);
			cgPatent.setPatentTitle(patentTitle);
			cgPatent.setPatentType(patentType);
			//set对应的教师编号
			List<Teacher> teacherList=teacherService.findByName(fuZeRen);
			if(teacherList.size()>0){
				cgPatent.setTeacherId(teacherList.get(0).getTeacherId());
			}
			chengguoService.updateCgPatent(cgPatent);
			request.setAttribute("cgPatent", cgPatent);
		}
		List<CgPatent> cgPatentlist = findAllPatentDepRole();		
		request.setAttribute("cgPatentlist", cgPatentlist);
		return SUCCESS;
	}
	
/**
 * 角色判断，管理员返回所有，教职工返回相应的
 * @return
 */
	private List<CgPatent> findAllPatentDepRole() {
		Teacher teacher=(Teacher)session.getAttribute("teacher");
		pageView=new PageView<CgPatent>(10,currentpage);
		
		QueryResult<CgPatent> queryResult =null;
		if(teacher.getRole()==1){
			queryResult = chengguoService.findAllCgPatent(pageView.getFirstResult(),pageView.getMaxresult());
		}else{
			queryResult = chengguoService.findAllCgPatentByTeacherId(teacher.getTeacherId(),pageView.getFirstResult(),pageView.getMaxresult());
		}
		pageView.setQueryResult(queryResult);
		request.setAttribute("pageView", pageView);
		return queryResult.getResultlist();
	}

}
